<?php
/**
 * Created by PhpStorm.
 * User: mww
 * Date: 2018/12/17
 * Time: 16:08
 */
namespace app\bo\controller;
use function PHPSTORM_META\type;
use think\Db;
class My extends Base
{
    /**
     * 我的
     * 1、已报备
     * 2、待更新
     * 3、待审核
     * 4、已加盟
     * 5、已过期
     */
    public function index()
    {
        session('base64ImageSrc',null);
        $user = Db::table('up_u_user')
            ->where('user_id',session('user_id'))
            ->find();
        $where = [
            's_u_userid' => $user['user_id'],
            's_type' => 2
        ];
        $where['s_verify'] = 2;
        $where['s_status'] = 1;
        $one = $data = Db::table('up_shop')
            ->where($where)
            ->count();
        $where = [
            's_u_userid' => $user['user_id'],
            's_type' => 2
        ];
        $three_days_ago = date('Y-m-d H:i:s',time()-(3600*24*3));
        $where['s_verify'] = 2;
        $where['s_status'] = 1;
        $where['s_update_time'] = ['<',$three_days_ago];
        $two = $data = Db::table('up_shop')
            ->where($where)
            ->count();
        $where = [
            's_u_userid' => $user['user_id'],
            's_type' => 2
        ];
        $where['s_verify'] = 1;
        $where['s_verify'] = ['<>',2];
        $three = $data = Db::table('up_shop')
            ->where($where)
            ->count();
        $where = [
            's_u_userid' => $user['user_id'],
            's_type' => 2
        ];
        $where['s_status'] = 2;
        $four = $data = Db::table('up_shop')
            ->where($where)
            ->count();
        $where = [
            's_u_userid' => $user['user_id'],
            's_type' => 2
        ];
        $where['s_status'] = ['>=',3];
        $five = $data = Db::table('up_shop')
            ->where($where)
            ->count();
        $where = [
            's_u_userid' => ['<>',$user['user_id']],
            's_type' => 2
        ];
        $where['s_inside_phone'] = $user['user_phone'];
        $six = $data = Db::table('up_shop')
            ->where($where)
            ->count();
        $this->assign('one',$one ?: '');
        $this->assign('two',$two ?: '');
        $this->assign('three',$three ?: '');
        $this->assign('four',$four ?: '');
        $this->assign('five',$five ?: '');
        $this->assign('six',$six ?: '');
        return $this->fetch();
    }

    /**
     * 查看报备详情
     */
    public function mylist()
    {
        extract(input());
        if(!isset($type) || !$type || $type=='undefined'){
            $type = 1;
        }
        if(!isset($level) || !$level || $level=='undefined'){
            $level = 1;
        }
        $this->assign('type',$type);
        $this->assign('level',$level);
        $this->assign('street','');
        return $this->fetch();
    }

    /**
     * 1、已报备
     * 2、待更新
     * 3、待审核
     * 4、已加盟
     * 5、已过期
     * @return \think\response\Json
     */
    public function mylistAjax()
    {
        extract(input());
        $user = Db::table('up_u_user')
            ->where('user_id',session('user_id'))
            ->find();
        if(!isset($type) || !$type){
            $type = 1;
        }
        if(!isset($level) || !$level){
            $level = 1;
        }
//        $where = [
//            's_u_userid' => $user['user_id'],
//        ];
//        $where2 = [
//            's_inside_phone' => $user['user_phone'],
//        ];

        if($level == 1){
            $where = [
                's_u_userid' => $user['user_id'],
            ];
        }else{
            $where = [
                's_inside_phone' => $user['user_phone'],
            ];
        }
        if($type == 1){ //已报备
            $where['s_verify'] = 2;
            $where['s_status'] = 1;
//            $where2['s_verify'] = 2;
//            $where2['s_status'] = 1;
        }elseif ($type == 2){ //待更新
            $three_days_ago = date('Y-m-d H:i:s',time()-(3600*24*3));
            $where['s_verify'] = 2;
            $where['s_status'] = 1;
            $where['s_update_time'] = ['<',$three_days_ago];
//            $where2['s_verify'] = 2;
//            $where2['s_status'] = 1;
//            $where2['s_update_time'] = ['<',$three_days_ago];
        }elseif ($type == 3){ //待审核
            $where['s_verify'] = ['<>',2];
//            $where2['s_verify'] = ['<>',2];
        }elseif ($type == 4){ //已加盟
            $where['s_status'] = 2;
//            $where2['s_status'] = 2;
        }elseif ($type == 5){ //已过期
            $where['s_status'] = ['>=',3];
//            $where2['s_status'] = ['>=',3];
        }
        if(isset($street) && $street){
            $id = explode('-',$street);
            $where['s_qu'] = $id[2];
            $where2['s_qu'] = $id[2];
        }
        $where['s_type'] = 2;

        $data = Db::table('up_shop')
            ->where($where)
//            ->whereOr(function ($query) use ($where2) {
//                $query->where($where2);
//            })
            ->field('s_id,s_u_userid,s_com_name,s_name,s_u_name,s_inside_name,s_status,s_verify,s_reason')
            ->order('s_status asc,s_verify asc,s_update_time desc')
            ->select();
        if(empty($data)){
            $data = '';
        }
        return json(['status' => 200,'msg' => $data]);
    }

    /**
     * 查看报备详情
     */
    public function info()
    {
        extract(input());
        $data = Db::table('up_shop')->alias('s')
            ->join('up_u_user u','s.s_u_userid=u.user_id','left')
            ->where('s_id', $sid)
            ->field('s.*,user_image')
            ->find();
        $this->assign('sid',$sid);
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 更新报备
     */
    public function update()
    {
        extract(input());
        $this->assign('sid',$sid);
        return $this->fetch();
    }

    /**
     * 更新报备
     */
    public function updateAjax()
    {
        extract(input());
        $userInfo = Db::table('up_u_user')->where('user_id',session('user_id'))->find();
        $config = Db::table('up_shop_config')->where('id',1)->find();
        $insert = [
            'si_u_name' => $userInfo['user_username'],
            'si_u_phone' => $userInfo['user_phone'],
            'si_u_userid' => $userInfo['user_id'] ,
            'si_s_id' => $sid ,
            'si_name' => $s_name ,
            'si_phone' => $s_phone ,
            'si_wechat' => $s_wx_name,
            'si_content' => $s_content,
            'si_remark' => $s_remark,
            'si_images' => $upload,
            'si_time' => date('Y-m-d H:i:s'),
            'si_update_time' => date('Y-m-d H:i:s')
        ];
        if($upload){
            $insert['si_days'] = $config['must']+$config['nomust'];
        }else{
            $insert['si_days'] = $config['must'];
        }
        if(!$s_name || !$s_phone || !$s_content){
            return json(['status'=>0,'msg'=>'有未填项，请检查']);
        }
        if(mb_strlen($s_content) < 50){
            return json(['status'=>0,'msg'=>'沟通要点字数不能少于50字']);
        }
        Db::startTrans();
        try{
            Db::table('up_shop_image')->insertGetId($insert);
            Db::commit();
            return json(['status'=>200,'msg'=>'更新成功！']);
        }catch (\Exception $e) {
            Db::rollback();
            return json(['status'=>0,'msg'=>'保存失败，请刷新重试']);
        }
    }

    /**
     * 个人中心
     */
    public function userSet()
    {
        $basedata = session('base64ImageSrc');
        if(!$basedata){
            $basedata = '';
        }
        $this->assign('basedata',$basedata);
        return $this->fetch('my/user_set');
    }

    public function AjaxUserCenter()
    {
        $user_id = $_POST['user_id'];
        $user_image = trim($_POST['user_image']);
        $user_phone = trim($_POST['user_gender']);
        $user_username = trim($_POST['user_username']);
        $user_address = trim($_POST['user_address']);
        $user_area_str = trim($_POST['user_area_str']);
        $areaid = trim($_POST['areaid']);
        $update = [
            'user_image' => $user_image ,
            'user_gender' => $user_phone ,
            'user_username' => $user_username ,
            'user_address' => $user_address ,
            'user_area_str' => $user_area_str ,
            'user_update_time' => date('Y-m-d H:i:s')
        ];
        if(!$user_phone || !$user_username){
            return json(['status'=>0,'msg'=>'姓名不能为空']);
        }
        if($areaid){
            $area = explode('-',$areaid);
            $update['user_sheng'] = $area[0];
            $update['user_shi'] = $area[1];
            $update['user_qu'] = $area[2];
        }
        $res = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->update($update);
        if($res){
            return json(['status'=>200,'msg'=>'更改成功']);
        }else{
            return json(['status'=>0,'msg'=>'更改失败']);
        }
    }

    public function base64GoImg()
    {
        $basedata = $_POST['basedata'];
        $res = base64_img($basedata);
        if($res['code'] == 200){
            session('base64ImageSrc',$res['msg']);
        }else{
            session('base64ImageSrc','');
        }
        return json($res);
    }

    /**
     * 修改密码
     */
    public function updatePass()
    {
        return $this->fetch('my/pass');
    }

    /**
     * 头像裁剪
     */
    public function goImage()
    {
        $imgsrc = input('imgsrc');
        $this->assign('imgsrc',$imgsrc);
        return $this->fetch('my/image');
    }

    public function AjaxUpdatePass()
    {
        $user_id = $_POST['user_id'];
        $user_pass = $_POST['user_pass'];
        $old = $_POST['old'];
        $new = $_POST['new'];
        $sure = $_POST['sure'];
        if(!$old || !$new || !$sure){
            return json(['status'=>0,'msg'=>'不能为空！']);
        }
        if($new != $sure){
            return json(['status'=>0,'msg'=>'新密码输入不一致！']);
        }
        if(strlen($new) < 6){
            return json(['status'=>0,'msg'=>'密码不得低于6位！']);
        }
        if($user_pass != md5($old)){
            return json(['status'=>0,'msg'=>'原密码错误！']);
        }
        $update = [
            'user_password' => md5($new) ,
            'user_update_time' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->update($update);
        if($res){
            return json(['status'=>200,'msg'=>'更改成功']);
        }else{
            return json(['status'=>0,'msg'=>'更改失败']);
        }
    }
}